package com.me.study.javaBasic.dataType;

import org.junit.Test;

/**
 * 测试 浮点型精度丢失
 */
public class TestDouble {
    double d1 = 0.1;
    double d2 = 0.1000000000000000055511151231257827021181583404541015625;
    double d3 = 0.1000000000000000055511151231257;
    double d4 = 0.100000000000000005;

    /**
     * 为什么说 浮点型 不等于 自然小数
     */
    @Test
    public void test1() {
        System.out.println("double1: " + d1);
        System.out.println("double2: " + d2);
        System.out.println("double3: " + d3);
        System.out.println("double4: " + d4);
        System.out.println("d1 是否等于 d2: " + (d1 == d2));
    }

    /**
     * 一些精度丢失的案例
     */
    @Test
    public void test2() {
        System.out.println("0.1 + 0.2 不是 0.3: " + (d1 + 0.2));
        System.out.println("0.0023 * 100 不是 0.23: " + (0.0023 * 100));
        System.out.println("19501.10 * 100 不是 1950110: " + (19501.10 * 100));
        System.out.println("19501.10 * 100000 不是 1950110000: " + (19501.10 * 100000));
        System.out.println("19501.10 ➗ 1000 不是 19.50110: " + (19501.10 / 1000));
    }

    /**
     * 为什么不能 乘以100、除以100 来计算，因为乘除法也会丢呀。。
     */
    @Test
    public void test3() {
        System.out.println("0.0023 * 100 不是 0.23: " + (0.0023 * 100));
        System.out.println("19501.10 * 100 不是 1950110: " + (19501.10 * 100));
        System.out.println("19501.10 * 100000 不是 1950110000: " + (19501.10 * 100000));
        System.out.println("19501.10 ➗ 1000 不是 19.50110: " + (19501.10 / 1000));
    }

}
